package com.westvalley.common.kit;

import java.util.ArrayList;
import java.util.List;

public class SqlKit{
	private String strTableName = "";	//表名称
	private List<String> listColumn = new ArrayList<String>();	//列集合
	private List<String> listValue = new ArrayList<String>();	//值集合
	private String strWhere = "";	//where条件
	private StringBuffer sql = new StringBuffer();
	
	/**
	 * 设置表名
	 * @param strTableName
	 */
	public void setTableName(String strTableName){
		this.strTableName=strTableName.toUpperCase();
	}
	
	/**
	 * 添加where条件
	 * @param strWhere
	 */
	public void setWhere(String strWhere){
		this.strWhere = strWhere;
	}
	
	/**
	 * 添加数据
	 * @param strColumn 列名
	 * @param strValue	值
	 */
	public void addStr(String strColumn,String strValue){
		listColumn.add(strColumn);
		listValue.add("'"+strValue+"'");
	}
	
	/**
	 * 添加数据
	 * @param strColumn 列名
	 * @param strValue	值
	 */
	public void addData(String strColumn){
		listColumn.add(strColumn);
		listValue.add("?");
	}
	
	/**
	 * 添加数据
	 * @param strColumn 列名
	 * @param strValue	值
	 */
	public void addDataDate(String strColumn,String format,String strValue){
		listColumn.add(strColumn);
		listValue.add("TO_DATE('"+format+"','"+strValue+"')");
	}
	
	/**
	 * 获取添加sql
	 * @return
	 */
	public String getInsertSql(){
		this.sql.append(" INSERT INTO ");
		this.sql.append(this.strTableName);
		this.sql.append(" ( ");
		this.sql.append( StringKit.list2String(this.listColumn, ",", false) );
		this.sql.append(" ) ");
		this.sql.append(" VALUES ");
		this.sql.append(" ( ");
		this.sql.append( StringKit.list2String(this.listValue, ",", false) );
		this.sql.append(" ) ");
		return sql.toString();
	}
	
	/**
	 * 获取更新sql
	 * @return
	 */
	public String getUpdateSql(){
		this.sql.append(" UPDATE ");
		this.sql.append(this.strTableName);
		this.sql.append(" SET ");
		StringBuffer str = new StringBuffer();
		for(int i=0;i<this.listColumn.size();i++){
			str.append(",");
			str.append(this.listColumn.get(i));
			str.append(" = ");
			str.append(""+this.listValue.get(i)+"");
		}
		this.sql.append(str.subSequence(1, str.length()));
		this.sql.append(" WHERE ");
		this.sql.append( this.strWhere );
		return this.sql.toString();
	}
	
	/**
	 * 获取删除sql
	 * @return
	 */
	public String getDeleteSql(){
		this.sql.append(" DELETE FROM ");
		this.sql.append(this.strTableName);
		if( !"".equals(this.strWhere)){
			this.sql.append(" WHERE ");
			this.sql.append( this.strWhere );
		}
		return this.sql.toString();
	}
	
	/**
	 * 清空对象
	 */
	public void clear(){
		this.strTableName = "";	//表名称
		this.listColumn = new ArrayList<String>();	//列集合
		this.listValue = new ArrayList<String>();	//值集合
		this.strWhere = "";	//where条件
		this.sql = new StringBuffer();
	}
	
}
